package it.xiuxian.xingqiu.fragment



import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import it.xiuxian.lib.base.ArouterAddress
import it.xiuxian.lib.base.BaseFragment
import it.xiuxian.lib.utils.ImageUtil
import it.xiuxian.lib.utils.adapter.SlimAdapter
import it.xiuxian.lib.utils.adapter.SlimInjector
import it.xiuxian.lib.utils.adapter.injector.IViewInjector
import it.xiuxian.xingqiu.R
import it.xiuxian.xingqiu.bean.GongYiBean
import it.xiuxian.xingqiu.bean.SuanLiBean
import it.xiuxian.xingqiu.databinding.FragmentXingqiuBinding
import it.xiuxian.xingqiu.presenter.XingQiuFragmentPresenter
import java.util.*


@Route(path = ArouterAddress.XINGQIUFRAGMENT)
class XingQiuFragment:BaseFragment<XingQiuFragmentPresenter>() {

    lateinit var binding:FragmentXingqiuBinding
   lateinit var adapter1: SlimAdapter
    override val layoutId: Int
        get() = R.layout.fragment_xingqiu
    override val presenter: XingQiuFragmentPresenter
        get() = XingQiuFragmentPresenter()

    override fun isEmpty(textView: TextView?): Boolean {
        return false
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        binding  = getViewDataBinding<FragmentXingqiuBinding>()
        setTitle("公益照片墙")
        binding.rv.layoutManager = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)
        adapter1 = SlimAdapter.create().register<GongYiBean>(
            R.layout.fragment_xingqiu_item,
            object : SlimInjector<GongYiBean>{
                override fun onInject(
                    data: GongYiBean?,
                    injector: IViewInjector<out IViewInjector<*>>?
                ) {

                    injector!!.text(R.id.tv_zhang, "共"+data!!.imageList.size.toString()+"张")
                        .text(R.id.tv_content, data.message)
                        .text(R.id.tv_address, data.address)
                        .clicked(R.id.image, View.OnClickListener {
                        ARouter.getInstance().build(ArouterAddress.BANNERACTIVITY).withSerializable("newlist",data.imageList).withString("id",data.id.toString()).navigation()
                        })
                    ImageUtil.setRoundImage(data.imgurl, injector.findViewById<ImageView>(R.id.image))


                }
            }).attachTo(binding.rv)
        binding.smart.setOnRefreshListener{
            page=1
            mPresenter!!.getList(page.toString(),"1")
            binding.smart.finishRefresh()
        }
        binding.smart.setOnLoadMoreListener{
            if(next!=-1){
                mPresenter!!.getList((++page).toString(),"1")
            }else{
                binding.smart.setNoMoreData(true)
            }
        }
    }





    public override fun initData(){
    mPresenter!!.getList("1","1")
    }

    override fun initEvent(){

    }

    var newList = ArrayList<GongYiBean>()
    var page =1
    var next =0
    fun geData(data: SuanLiBean){
        Log.e("newdata",data.toString())
        if(data.list.isNotEmpty()){
            if(page==1){
                newList.clear()
                newList.addAll(data.list)
            }else{
                newList.addAll(data.list)
            }
          next = data.next
            adapter1.updateData(newList)
        }
        binding.smart.finishLoadMore()
    }


}